package com.example.assisteddriving;

import static android.content.Context.MODE_PRIVATE;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Wode extends Fragment {
    TextView xingbie, shengri, dizhi, shengao, tizhong;
    LinearLayout llxingbie, llshengri, lldizhi, llshengao, lltizhong;
    ImageView bianjiback;
    Button baocun;
    EditText xingming, dianhua, jinjidianhua;
    public String PREFERENCE_NAME = "gerenxinxi";  //文件名
    public int MODE = MODE_PRIVATE;  //操作模式
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wode, null);
        initview(view);

        return view;
    }

    private void initview(View view) {
        llxingbie = view.findViewById(R.id.llxingbie);
        xingbie = view.findViewById(R.id.xingbie);
        llxingbie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final List<String> listData = getxingbieData();
//      监听选中
                OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
//               返回的分别是三个级别的选中位置
//              展示选中数据
                        xingbie.setText(listData.get(options1));
                    }
                })
                        .setSelectOptions(0)//设置选择第一个
                        .setOutSideCancelable(false)//点击背的地方不消失
                        .build();//创建
//      把数据绑定到控件上面
                pvOptions.setPicker(listData);
//      展示
                pvOptions.show();

            }
        });
        llshengao = view.findViewById(R.id.llshengao);
        shengao = view.findViewById(R.id.shengao);
        llshengao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final List<String> listData = getshengaoData();
//      监听选中
                OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
//               返回的分别是三个级别的选中位置
//              展示选中数据
                        shengao.setText(listData.get(options1));
                    }
                })
                        .setSelectOptions(60)//设置选择第一个
                        .setOutSideCancelable(false)//点击背的地方不消失
                        .build();//创建
//      把数据绑定到控件上面
                pvOptions.setPicker(listData);
//      展示
                pvOptions.show();
            }
        });
        lltizhong = view.findViewById(R.id.lltizhong);
        tizhong = view.findViewById(R.id.tizhong);
        lltizhong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final List<String> listData = gettizhongData();
//      监听选中
                OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
//               返回的分别是三个级别的选中位置
//              展示选中数据
                        tizhong.setText(listData.get(options1));
                    }
                })
                        .setSelectOptions(0)//设置选择第一个
                        .setOutSideCancelable(false)//点击背的地方不消失
                        .build();//创建
//      把数据绑定到控件上面
                pvOptions.setPicker(listData);
//      展示
                pvOptions.show();
            }
        });
        llshengri = view.findViewById(R.id.llshengri);
        shengri = view.findViewById(R.id.shengri);
        llshengri.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //时间选择器
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String text = year + "年" + (month + 1) + "月" + dayOfMonth + "日";
                        shengri.setText(text);
                    }
                }
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH)).show();


            }
        });
        xingming = view.findViewById(R.id.xingming);
        dianhua = view.findViewById(R.id.dianhua);
        jinjidianhua = view.findViewById(R.id.jinjidianhua);

    }


    @Override
    public void onStart() {
        super.onStart();
        SharedPreferences sharedPreferences =
                getContext().getSharedPreferences(PREFERENCE_NAME, MODE);

        xingming.setText(sharedPreferences.getString("xingming", null));
        xingbie.setText(sharedPreferences.getString("xingbie", null));
        shengao.setText(sharedPreferences.getString("shengao", null));
        tizhong.setText(sharedPreferences.getString("tizhong", null));
        shengri.setText(sharedPreferences.getString("shengri", null));
        dianhua.setText(sharedPreferences.getString("dianhua", null));
        jinjidianhua.setText(sharedPreferences.getString("jinjidianhua", null));
    }

    @Override
    public void onPause() {
        super.onPause();
        SharedPreferences sharedPreferences =getContext().getSharedPreferences(PREFERENCE_NAME, MODE);

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("xingming", String.valueOf(xingming.getText()));
        editor.putString("xingbie", String.valueOf(xingbie.getText()));
        editor.putString("shengao", String.valueOf(shengao.getText()));
        editor.putString("tizhong", String.valueOf(tizhong.getText()));
        editor.putString("shengri", String.valueOf(shengri.getText()));
        editor.putString("dianhua", String.valueOf(dianhua.getText()));
        editor.putString("jinjidianhua", String.valueOf(jinjidianhua.getText()));
        editor.commit();
    }

    @Override
    public void onStop() {
        super.onStop();

    }

    private List<String> getxingbieData() {
        List<String> list = new ArrayList<>();
        list.add("男");
        list.add("女");
        list.add("保密");
        return list;

    }
    private List<String> getshengaoData() {
        List<String> list = new ArrayList<>();
        for (int i=100;i<250;i++){
            list.add(String.valueOf(i));
        }
        return list;

    }
    private List<String> gettizhongData() {
        List<String> list = new ArrayList<>();
        for (int i=100;i<300;i++){
            list.add(String.valueOf(i)+"斤");
        }
        return list;

    }
}